package com.cskaoyan.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author stone
 * @date 2022/06/01 23:51
 */
@Component
public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (request.getSession().getAttribute("username") != null) {
            return true;
        }
        if (request.getRequestURI().endsWith("login")) {
            return true;
        }
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("登录失败");// String default Charset=iso-8859-1
        return false;
    }
}
